//
//  CarInfoItemView.m
//  BalanceCar
//
//  Created by 郑信鸿 on 17/6/22.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "CarInfoItemView.h"
#import "NSAttributedString+text.h"

@interface CarInfoItemView ()

@property(nonatomic, strong)UILabel *valueLabel;
@property(nonatomic, strong)NSAttributedString *unitAttr;

@end

@implementation CarInfoItemView{
    
    NSString *_title;
    NSString *_iconName;
    NSString *_unit;
    
}


- (instancetype)initWithTitle:(NSString *)title iconName:(NSString *)iconName unit:(NSString *)unit{
    
    if (self = [super init]) {
        
        _title = title;
        _iconName = iconName;
        _unit = unit;
        NSDictionary *attibutes = @{
                                    NSForegroundColorAttributeName:[UIColor lightGrayColor],
                                    NSFontAttributeName:[UIFont systemFontOfSize:14]
                                    };
        self.unitAttr = [[NSAttributedString alloc] initWithString:unit attributes:attibutes];
        [self establishSubViews];
    }
    
    return self;
}

- (void)establishSubViews{
    
    UIImageView *iconImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:_iconName]];
    [self addSubview:iconImage];
    [iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(0);
        make.centerX.mas_offset(0);
    }];
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.textColor = [UIColor colorFromHexCode:@"#666666"];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = _title;
    [self addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(iconImage.mas_bottom).offset(15);
        make.left.right.mas_offset(0);
        make.height.mas_offset(15);
    }];
    self.valueLabel = [[UILabel alloc] init];
    [self addSubview:self.valueLabel];
    self.valueLabel.textAlignment = NSTextAlignmentCenter;
    [self.valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(15);
        make.left.right.mas_offset(0);
        make.bottom.mas_offset(0);
    }];
    self.value = @"0.0";
}


- (void)setValue:(NSString *)value{
    _value = value;
    NSDictionary *attibutes = @{
                                NSForegroundColorAttributeName:[UIColor colorFromHexCode:@"#333333"],
                                NSFontAttributeName:[UIFont systemFontOfSize:18]
                                };
    NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:value attributes:attibutes];
    [attriStr appendAttributedString:self.unitAttr];
    self.valueLabel.attributedText = attriStr;
    
}


@end
